﻿#include "BrowserTableHandlerLoadMonopole.h"
#include "OperatorsInterface/OperAbstractLoad.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadExcitationMonopole.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QCheckBox>

namespace GUI
{

    void BrowserTableHandlerLoadMonopole::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Enabled")<< tr("Pressure Spectrum") 
            << tr("X[m]") << tr("Y[m]") << tr("Z[m]") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }
    void BrowserTableHandlerLoadMonopole::updateData()
    {
        if (_tableWidget == nullptr) return;
        if (_loadMgr == nullptr)return;

        QList<Acoustics::FITKAcousticsAbstractLoad*> list = _loadMgr->getLoadByType(
            Acoustics::FITKAcousticsAbstractLoad::ExcitationMonopole);

        //载荷数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换单极子
            Acoustics::FITKLoadExcitationMonopole* mpl = dynamic_cast<Acoustics::FITKLoadExcitationMonopole*>(list.at(i));
            if (mpl == nullptr) continue;

            //数据ID
            const int id = mpl->getDataObjectID();

            _tableWidget->setItem(i, 0, new QTableWidgetItem(mpl->getDataObjectName(), id));
            QCheckBox* box = new QCheckBox("");
            box->setChecked(mpl->getEnabledState());
            connect(box, &QCheckBox::stateChanged, this, [mpl, id](int state) {
                if (state == Qt::Checked)  mpl->setEnabledState(true);
                else if (state == Qt::Unchecked)  mpl->setEnabledState(false);
                EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
                if (operGraph)operGraph->updateGraph(id);
            });
            _tableWidget->setCellWidget(i, 1, box);

            QString spec = "None";
            if (mpl->getSpectraDataObj()) spec = mpl->getSpectraDataObj()->getDataObjectName();
            _tableWidget->setItem(i, 2, new QTableWidgetItem(spec, id));
            double location[3]{};
            mpl->getConnectionLocation(location);
            _tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(location[0]), id));
            _tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(location[0]), id));
            _tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(location[0]), id));
            _tableWidget->setItem(i, 6, new QTableWidgetItem(mpl->getNotes(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }
    EventOper::OperAbstractLoad * BrowserTableHandlerLoadMonopole::getLoadOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractLoad>("OperLoadMonopole");
    }
    bool BrowserTableHandlerLoadMonopole::enableAttriSet()
    {
        //再说
        return false;
    }
}